# 下载器
import requests  # requests会自动根据网页头部解码，和默认utf-8解码(前者不知真假)


class HtmlDownloader(object):

    def download(self, url):
        # 判断url是否为空 第一种写法
        assert url is not None, 'download()方法参数url不能为None'

        # if url is None:  # 第二种写法
        #     return None
        # if url is None:  # 第三种写法
        #     raise Exception("参数url不能为None")

        headers = {
            'Host': 'baike.baidu.com',
            'Referer': 'https://baike.baidu.com/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36'
        }

        response = requests.get(url, headers=headers)
        if response.status_code != 200:
            raise Exception(f'请求失败，code{response.status_code}')
        return response.content.decode(encoding='utf-8')
